<template>
    <el-card shadow="never" class="border-0">
        <!-- 搜索 -->
        <el-form :model="searchForm" label-width="80px" class="mb-3" size="small">
            <el-row :gutter="20">
                <el-col :span="7" :offset="0">
                    <el-form-item label="考试名称">
                        <el-input v-model="searchForm.examName" placeholder="考试名称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="15">
                    <div class="flex items-center justify-end">
                        <el-button type="primary" @click="getData">搜索</el-button>
                        <el-button @click="resetSearchForm">重置</el-button>
                    </div>
                </el-col>
            </el-row>
        </el-form>


        <el-table :data="tableData" stripe style="width: 100%" v-loading="tableDataLoading">

            <el-table-column label="考试名称" align="center" show-overflow-tooltip>
                <template #default="{ row }">
                    {{ row.examName }}
                </template>
            </el-table-column>

            <el-table-column label="考试描述" align="center" show-overflow-tooltip>
                <template #default="{ row }">
                    {{ row.examDescription }}
                </template>
            </el-table-column>

            <el-table-column label="考试时间限制" align="center" show-overflow-tooltip>
                <template #default="{ row }">
                    {{ row.timeLimit }}
                </template>
            </el-table-column>

            <el-table-column label="满分" align="center">
                <template #default="{ row }">
                    {{ row.totalScore }}
                </template>
            </el-table-column>

            <el-table-column label="及格分数" align="center">
                <template #default="{ row }">
                    {{ row.passScore }}
                </template>
            </el-table-column>

            <!-- <el-table-column label="对应证书" align="center">
                <template #default="{ row }">
                    {{ row.cert }}
                </template>
            </el-table-column> -->

            <el-table-column label="创建时间" align="center">
                <template #default="{ row }">
                    {{ dateFormatter(row.createTime) }}
                </template>
            </el-table-column>

            <el-table-column label="操作" width="100" align="center">
                <template #default="scope">
                    <el-button v-if="showBtnName(scope.row.configId) != -1" type="success" size="small" text
                        @click="submitAssign(scope.row)">已选择</el-button>
                    <el-button v-else type="primary" size="small" text @click="submitAssign(scope.row)">未选择</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <div class="flex items-center justify-center mt-5">
            <el-pagination background layout="prev, pager ,next" :total="total" :current-page="currentPage"
                :page-size="limit" @current-change="getData" />
        </div>
    </el-card>
</template>

<script setup>
import { ref, onMounted, reactive, defineProps } from 'vue'
import { dateFormatter } from '~/utils/common.js'

// 列表数据
const tableData = ref([])
const currentPage = ref(1)
const total = ref(0)
const limit = ref(5)
const tableDataLoading = ref(false)
import { api_examConfig_list } from "~/api/exam/examPaper"

// props
const props = defineProps({
    //已选择的考试ids
    officialExamId: null,
})

// 已选择的数据ids
const selectedDataIds = ref([])

// 查询条件
const searchForm = reactive({
    examName: null,
    page: currentPage.value,
    size: limit.value,
})

// 生命周期
onMounted(async () => {

    //已经选择的进行赋值
    if (null != props.officialExamId && undefined != props.officialExamId && props.officialExamId.length>0) {
        selectedDataIds.value = props.officialExamId
    }

    //加载表格数据
    await getData()
})

//获取列表数据
async function getData(p = null) {
    if (typeof p == "number") {
        currentPage.value = p
    }
    tableDataLoading.value = true
    searchForm.page = currentPage.value
    api_examConfig_list(searchForm)
        .then(res => {
            if (res) {
                tableData.value = res.records
                total.value = Number(res.total)
            } else {
                tableData.value = []
                total.value = 0
            }
        })
        .finally(() => {
            tableDataLoading.value = false
        })
}

// 重置搜索框
const resetSearchForm = () => {
    searchForm.examName = null
    getData()
}

const showBtnName = (id) => {
    let index = selectedDataIds?.value?.findIndex(x => x == id)
    return index
}

// 选择|取消
function submitAssign(row) {
    let index = selectedDataIds?.value.findIndex(x => x == row.configId)
    if (index != -1) {
        //取消选择
        selectedDataIds.value.splice(index, 1)
    } else {
        //确认选择
        selectedDataIds.value.push(row.configId)
    }
    //传递给调用者
    getCheckedExam()
}

// 对外暴露方法
const emit = defineEmits([
    'getCheckedExam',
])

const getCheckedExam = () => {
    const selectData = selectedDataIds
    let returnParams = selectData.value
    // console.log(JSON.stringify(returnParams))
    emit("getCheckedExam", returnParams)
}
</script>

<style>
.el-table__header-wrapper .el-checkbox {
    display: none
}
</style>
